
package com.niiwoo.civet.trade.service.local.intelligenceInvest;

import static org.springframework.transaction.annotation.Isolation.READ_COMMITTED;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.activity.stage.dto.request.prize.PackageInvestPrizeOccupiedReqDTO;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectFlowed;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestPrize;
import com.niiwoo.civet.trade.dao.entity.ProjectPackage;
import com.niiwoo.civet.trade.dao.mapper.ProjectFlowedMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestPrizeMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectPackageDetailMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectPackageInvestOrderMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectPackageMapperExt;
import com.niiwoo.civet.trade.dto.request.ProjectFlowRequestDTO;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.enums.Intelligence.ProjectPackageStatusEnum;
import com.niiwoo.civet.trade.service.project.ProjectDubboService;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class IntelligenceInvestFlowService {

	@Reference(version = "1.0.0")
	private ProjectDubboService projectDubboService;

	@Autowired
	private ProjectPackageMapperExt projectPackageMapperExt;
	@Autowired
	private ProjectPackageDetailMapperExt projectPackageDetailMapperExt;
	@Autowired
	private ProjectMapperExt projectMapperExt;
	@Autowired
	private ProjectPackageInvestOrderMapperExt projectPackageInvestOrderMapperExt;
	@Autowired
	private ProjectFlowedMapperExt projectFlowedMapperExt;
	@Autowired
	private SnowflakeIdWorker snowflakeIdWorker;
	
	@Autowired
	private ProjectInvestPrizeMapperExt projectInvestPrizeMapperExt;
	
	/**
	 * 智能投流包
	 * 
	 * @param projectPackageId
	 */
	@Transactional(isolation = READ_COMMITTED)
	public void flowPackage(Long projectPackageId) {
		//存在交易中的或者关闭中的订单，不处理流标，流标前提确认所有交易
		int hasInvesting = projectPackageInvestOrderMapperExt.countInvesting(projectPackageId);
		if(hasInvesting >0) {
			return  ;
		}
		ProjectPackage projectpackage = projectPackageMapperExt.selectByPrimaryKey(projectPackageId);
		// 不存在包或者包不是投资中，不处理
		if (Objects.isNull(projectpackage) || !ProjectPackageStatusEnum.INVESTING.getStatus().equals(projectpackage.getStatus())) {
			return;
		}
		List<Long> projectIds = projectPackageDetailMapperExt.selectProjectIdByPackageId(projectPackageId);
		//该智能投包下面所有的标
		List<Project> projects = projectMapperExt.selectListByProjectIdSet(projectIds);
		for (Project project : projects) {
			Long projectId = project.getProjectId();
			// 只处理筹款中的标
			if (ProjectSubStatusEnum.COLLECTING_INIT.getSubStatus() != project.getSubStatus()) {
				continue;
			}
			// 流标
			ProjectFlowRequestDTO projectFlowRequestDTO = new ProjectFlowRequestDTO();
			projectFlowRequestDTO.setProjectId(projectId);
			projectFlowRequestDTO.setSubStatus(ProjectSubStatusEnum.MISCARRY_INVEST_OVERTIME);
			projectFlowRequestDTO.setRemark(ProjectSubStatusEnum.MISCARRY_INVEST_OVERTIME.getDesc());
			try {
				//是否已经触发过流标，已经触发的不需要触发，ProjectFlowSyncJobTrigger 会自动触发
				ProjectFlowed projectFlowed=projectFlowedMapperExt.selectByPrimaryKey(projectId);
				if(Objects.nonNull(projectFlowed)) {
					continue;
				}
				//触发流标
				//包投资订单的数据变更，将在投资订单撤投修改investService.investCancelForProject
				projectDubboService.flow(projectFlowRequestDTO);
			} catch (Exception e) {
				// 存在失败的流标，包的状态不会修改成最终状态，后续定时任务继续触发
				log.info("智能投流包，包Id{},标的Id{}流标失败,错误信息{}", projectPackageId, project.getProjectId(), e.getMessage());
			}
		}
		
	}

	/**
	 * 智能投红包回退
	 * @param prizeOccupiedReqDTO
	 */
	@Transactional
	public void generatePrizeRedBack(PackageInvestPrizeOccupiedReqDTO prizeOccupiedReqDTO) {
		ProjectInvestPrize projectInvestPrize = new ProjectInvestPrize();
		projectInvestPrize.setId(snowflakeIdWorker.nextId());
		projectInvestPrize.setCreateTime(new Date());
		projectInvestPrize.setInvestOrderId(prizeOccupiedReqDTO.getPackageInvestOrderId());
		projectInvestPrize.setInvestType(Byte.valueOf("3"));
		projectInvestPrize.setOrderNo(prizeOccupiedReqDTO.getOrderNo());
		projectInvestPrize.setPrizeId(prizeOccupiedReqDTO.getPrizeId());
		projectInvestPrize.setProjectId(prizeOccupiedReqDTO.getPackageId());
		projectInvestPrize.setStatus(Byte.valueOf("0"));//处理中
		projectInvestPrize.setType(Byte.valueOf("2"));//回退
		projectInvestPrize.setUpdateTime(new Date());
		projectInvestPrize.setUserId(prizeOccupiedReqDTO.getUserId());
		projectInvestPrizeMapperExt.insertSelective(projectInvestPrize);
	}
	
	
	
}
